package com.oauth.service.impl;

import com.google.common.base.Strings;
import com.oauth.config.OauthConfig;
import com.oauth.error.OauthError;
import com.oauth.exception.OAuthErrorException;
import com.oauth.model.OpenToken;
import com.oauth.service.OauthAuthorizeService;
import com.oauth.service.OpenClientService;
import com.oauth.service.OpenTokenService;
import com.oauth.util.AuthUtil;
import org.apache.oltu.oauth2.as.issuer.MD5Generator;
import org.apache.oltu.oauth2.as.issuer.OAuthIssuerImpl;
import org.apache.oltu.oauth2.common.OAuth;
import org.apache.oltu.oauth2.common.error.OAuthError;
import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
import org.apache.oltu.oauth2.common.message.types.ResponseType;
import org.apache.oltu.oauth2.common.message.types.TokenType;
import org.apache.oltu.oauth2.common.token.BasicOAuthToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 授权服务
 * Created by yangjianbo on 2016/9/27.
 */
@Service
public class OauthAuthorizeServiceImpl implements OauthAuthorizeService {

    @Autowired
    private OpenClientService openClientService;

    @Autowired
    private OpenTokenService openTokenService;

    @Override
    public String authorizeByCode(String client_id,String redict_uri,String scope) throws OAuthProblemException,OAuthSystemException {
        if(!openClientService.isExisted(client_id)){
            throw  OAuthErrorException.error(OauthError.errorModel_client_not_exited);
        }else if(!openClientService.isUsed(client_id)){
            throw  OAuthErrorException.error(OauthError.errorModel_client_not_useed);
        }else if(Strings.isNullOrEmpty(redict_uri)){
            throw  OAuthErrorException.error(OauthError.errorModel_redicturi_not_empty);
        }else if(false){
            throw  OAuthErrorException.error(OauthError.errorModel_scope_out);
        }else {
            OpenToken openToken=new OpenToken();
            openToken.setClientId(client_id);
            openToken.setResponseType(ResponseType.CODE.toString());
            openToken.setCode(AuthUtil.getMd5Code());
            openToken.setRedirectUri(redict_uri);
            openToken.setScope(scope);
            openToken.setStatus(1);
            openTokenService.insert(openToken);
            return  openToken.getCode();
        }
    }

    @Override
    public BasicOAuthToken authorizeByToken(String client_id, String redict_uri, String scope) throws OAuthSystemException, OAuthProblemException {
        if(!openClientService.isExisted(client_id)){
            throw  OAuthErrorException.error(OauthError.errorModel_client_not_exited);
        }else if(!openClientService.isUsed(client_id)){
            throw  OAuthErrorException.error(OauthError.errorModel_client_not_useed);
        }else if(Strings.isNullOrEmpty(redict_uri)){
            throw  OAuthErrorException.error(OauthError.errorModel_redicturi_not_empty);
        }else if(false){
            throw  OAuthErrorException.error(OauthError.errorModel_scope_out);
        }else {
            OpenToken openToken=new OpenToken();
            openToken.setClientId(client_id);
            openToken.setResponseType(ResponseType.TOKEN.toString());
            openToken.setAccessToken(AuthUtil.getMd5Code());
            openToken.setRedirectUri(redict_uri);
            openToken.setScope(scope);
            openToken.setStatus(4);   //简单模式
            openTokenService.insert(openToken);
            BasicOAuthToken basicOAuthToken=new BasicOAuthToken(
                    openToken.getAccessToken(),TokenType.BEARER.toString(),OauthConfig.ACCESS_TOKEN_VALIDITY_SECONDS+0l,scope);
            return  basicOAuthToken;
        }
    }

    @Override
    public Boolean checkAuth(String client_id, String scope) {
        return null;
    }

}
